package chaw.android.train;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;

public class MoreActivity extends ActivityGroup {
	private ArrayList<String> mIdList;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
        
		if(mIdList == null) {
			mIdList = new ArrayList<String>();
			startChildActivity("moreHome", new Intent(getBaseContext(), MoreHomeActivity.class));
		}
	}
	
	@Override
	public void finishFromChild(Activity child) {
		// TODO Auto-generated method stub
//		super.finishFromChild(child);
		LocalActivityManager manager = getLocalActivityManager();
		int index = mIdList.size()-1;

		if (index < 1) {
			finish();
			return;
		}
		
		manager.destroyActivity(mIdList.get(index), true);
		mIdList.remove(index);
		index--;
		
		String lastId = mIdList.get(index);
		Intent lastIntent = manager.getActivity(lastId).getIntent();
		
		Window newWindow = manager.startActivity(lastId, lastIntent);
		setContentView(newWindow.getDecorView());
	}
	
	public void startChildActivity(String Id, Intent intent) {
		Window window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
		if (window != null) {
		mIdList.add(Id);
		setContentView(window.getDecorView());
		}
	}
	
	@Override
	public void onBackPressed() {
		finishFromChild(MoreActivity.this);
	}
}
